<?php

function get_register_email_subject($name) {
    return sprintf("Please verify your email address, %s", $name);
    // return sprintf(elgg_echo('register:email:subject'), $name);
}

function get_register_email_body($name, $username, $password, $link) {
    $app = get_input('client_id');
    if (strpos($app, WHITEBOARD) === 0) {
        $app = "Whiteboard";
    } else if (strpos($app, OPINIONS) === 0) {
        $app = "Opinions";
    } else if (strpos($app, DRAW_TOGETHER) === 0) {
        $app = "Draw Together";
    } else {
        $app = "ID";
    }
    return sprintf("
Dear %s,

Thanks for registering a Greengar %s account.

Your password is: %s

Please confirm your email address by clicking on the link below:

%s

If you can't click on the link, copy and paste it to your browser manually.

Best regards,

Greengar Studios
", $name, $app, $password, $link);
    // return sprintf(elgg_echo('register:email:body'), $name, $username, $password, $link);
}

function get_register_email_subject_confirm($name) {
    return sprintf(elgg_echo('confirm_email:email:subject'), $name);
}

function get_register_email_body_confirm($name, $username, $password, $link) {
    return sprintf(elgg_echo('confirm_email:email:body'), $name);
}

function greengar_register($email, $username, $password, $firstname, $lastname, $mail = TRUE) {
    // return export_result(200, 2);
    global $CONFIG;

    if (!$password || strlen($password) < 6) {
        $password = genRandomString(8);
//        $password = '12345678';
    }

    if (!$username || $mail) {
        $username = $email;
    }
//
    // Strip bad characters
    $blacklist2 = '\'/\\"*& ?#%^(){}[]~?<>;|¬¨`@-+=';
//    $blacklist2 = '@';
    for ($n = 0; $n < strlen($blacklist2); $n++) {
        $pos = strpos($username, $blacklist2[$n]);
        if ($pos != false) {
            $username = str_replace($blacklist2[$n], '_', $username);
        }
    }

//    $username = genRandomString(32);
//    $username = $email;


    if ($firstname && $lastname) {
        $name = $firstname . ' ' . $lastname;
    } else if ($firstname || $lastname) {
        if ($firstname) {
            $name = $firstname;
        } else {
            $name = $lastname;
        }
    } else {
        $name = $username;
        $pos = strpos($name, '_');
        $name = substr($name, 0, $pos);
    }

//    die(json_encode(export_result(400, "($username, $password, $name, $email)")));

    if (!$CONFIG->disable_registration) {
        try {
            $guid = register_user($username, $password, $name, $email, false);
        } catch (Exception $r) {
            return export_result(400, 'This email address has already been registered.'/* $r->getMessage() */);
        }
        if ($guid) {
            $new_user = get_entity($guid);
//            login($new_user);
            set_user_validation_status($guid, true);
            $new_user->enable();
            $new_user->setMetaData('greengar', 1);

            if (get_input('client_id') == WHITEBOARD) {
                greengar_add_relationship_following($guid, 47496, WHITEBOARD, TRUE);
                greengar_add_relationship_following(47496, $guid, WHITEBOARD, FALSE);
            }

            // request validation
            if ($mail) {
                add_email($guid, $email);
                $code = urlencode(compute_hash($guid . ',' . $new_user->email . ',' . $user->salt, REGISTER));
                $new_user->setMetaData('confirm_email', urldecode($code));
                $new_user->save();
                $link = "{$CONFIG->site->url}pg/greengar/user/confirm_email?user_id=$guid&code=$code";
                $app = get_input('client_id');
                if (strpos($app, WHITEBOARD) === 0) {
                    $app = "Whiteboard";
                } else if (strpos($app, OPINIONS) === 0) {
                    $app = "Opinions";
                } else if (strpos($app, DRAW_TOGETHER) === 0) {
                    $app = "Draw Together";
                } else {
                    $app = "ID";
                }
                $subject = "$app - Please verify your email address"; //get_register_email_subject($new_user->name);
                $body = get_register_email_body($name, $username, $password, $link);
                $result = notify_user($new_user->guid, $CONFIG->site->guid, $subject, $body, NULL, 'email');
            } else {
                // associate email if no require confirmation
                // add_email($guid, $email);
            }

            return export_result(200, $guid);
        } else {
            return export_result(400, elgg_echo("registerbad"));
        }
    } else {
        return export_result(500, elgg_echo('registerdisabled'));
    }
}

function greengar_confirm_email($userid, $code) {

    $user = get_user($userid);

    global $CONFIG;

    $check = ($code == $user->getMetaData('confirm_email'));
    //echo $code . ' vs ' . $user->getMetaData('confirm_email');

    if ($check) {

        // associate email after confirmation
        // add_email($user->guid, $user->email);

        $user->setMetaData('verify_email', $user->email);
        $user->save();
        $subject = get_register_email_subject_confirm($user->name);
        $body = get_register_email_body_confirm($user->name);

        $result = notify_user($user->guid, $CONFIG->site->guid, $subject, $body, NULL, 'email');
        return true;
    } else {
        return false;
    }
}